<?php
	include_once("../connect.php");
	include_once('../post_model.php');
	include_once('../user_model.php');
	include('../do_listing.php');
	session_start();
	mysql_select_db("progin_171_13509047",$con) or die(mysql_error());
	
	
	$op = explode("@",$_POST["operation"]);
	$p_model = new Post_Model();
	$uid = (isset($_SESSION["userid"])) ? $_SESSION["userid"]:"";
	$tag = (isset($_POST["tag"]))? $_POST["tag"]:"";
	$filtermode = (isset($_POST['filtermode']))? $_POST['filtermode']:"";
	$sortmode = (isset($_POST['sortmode']))? $_POST['sortmode']:"";
	$last = (isset($_POST['last']))? $_POST['last']:0;//untuk navigasi page
	$maxyscroll=(isset($_POST["maxyscroll"]))? $_POST["maxyscroll"]:0;//maximum post yang ditampilkan di daftar kontent
	$permittoquery=(isset($_POST["permission"]))? $_POST["permission"]:false;//maximum post yang ditampilkan di daftar kontent
	
	$maxinpage = 10;
	if($op[0] == "like" or $op[0] == "dislike")
	{
		if(!empty($uid)) //jika sudah login maka boleh melakukan like
		{
			if(!$p_model->isPostLikedByUser($uid,$op[1]))
			{
				$q = $p_model->doOperation($_POST["operation"]);
				$p_model->fetchLikeQuery($q,$op[1],$uid,$op[0]); 
			}
			else if ($p_model->isUserOperationSame($uid,$op[1],$op[0]))//netralkan nilai
			{
				echo $p_model->neutralizeLikeCount($uid,$op[1],$op[0]);
			}
			else if (!$p_model->isUserOperationSame($uid,$op[1],$op[0]))//lakukan penetralan dan operasi kebalikan
			{
				$invertop = ($p_model->getLikeOrDislike($uid,$op[1]) =="like")? "dislike":"like";
				$p_model->neutralizeLikeCount($uid,$op[1],$p_model->getLikeOrDislike($uid,$op[1]));
				$q = $p_model->doOperation($invertop."@".$op[1]);
				$p_model->fetchLikeQuery($q,$op[1],$uid,$invertop);
			}
		}
		else echo"error Please Login or Register First ";
	}
	else if ($op[0] == "scroll" and $op[1]>=$maxyscroll and $permittoquery =="false")
	{
		echo "plus ";
		$last+=$maxinpage;
		$permittoquery="true";
	}
	if ($op[0] == "scroll" and $op[1]>=$maxyscroll and $permittoquery == "true")
	{
		$query = $p_model->generateQuery($tag,$filtermode,$sortmode,$last,$maxinpage);
		$count_query = $p_model->generateCountQuery($tag,$filtermode,$sortmode,$last,$maxinpage);
		$dbcontent_query = mysql_query($query) or die(mysql_error());
		$dbcount = mysql_query($count_query) or die(mysql_error());
		$datarow = mysql_num_rows($dbcount);
		/* $temp_next = ($last<=$datarow? $last+$maxinpage : $last); */
		if($last < $datarow)
		{
			convertQuery($dbcontent_query,$uid);
/* 			echo "	<div class=contentbox>
				<div class=nav-container>		</div>
			</div>	"; */
		}
	}
	else if($op[0]=="scroll" and $op[1]<=$maxyscroll) echo "error";
?>